package dao;

import java.sql.SQLException;
import java.util.List;
import model.Horario;
import model.Professor;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

public class HorarioDAO {
    
    private static HorarioDAO instance = new HorarioDAO();

    public static HorarioDAO getInstence() {
        return instance;
    }

    private HorarioDAO() {
    }

    public void gravarHorario(Horario horario) throws SQLException, ClassNotFoundException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.save(horario);
        transaction.commit();
    }

    public void excluirHorario(Horario horario) throws SQLException, ClassNotFoundException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.clear();
        session.delete(horario);
        transaction.commit();
    }

    public List<Horario> obterHorarios() throws ClassNotFoundException, SQLException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Horario> horarios = session.createCriteria(Professor.class).list();
        return horarios;
    }
    
    public List<Horario> obterHorariosPorTurma(int codTurma) throws ClassNotFoundException, SQLException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Horario> horarios = session.createQuery("from Horario where codTurma="+codTurma).list();
        return horarios;
    }

    public void editarHorario(Horario horario) throws SQLException, ClassNotFoundException {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.clear();
        session.update(horario);
        transaction.commit();
    }
}
